<?php
namespace app\index\controller;
use \think\Controller;
use \app\index\model\Vercode;
class Reg extends Common
{
    public function index()
    {

        
        if(session('uid')){
            $this->error('你已经登录，请先退出','index/index/index');
        }

    	// var_dump($_SERVER['PHP_SELF']);

    	// var_dump(rtrim(dirname(rtrim(str_replace($_SERVER['HTTP_HOST'],'',explode('.php',$_SERVER['PHP_SELF'])[0].'.php'),'/')),'/'));
    	// exit;

    	$question = model('vercode')->getRandOne();

        return $this->fetch('',['question'=>$question,'title'=>'注册']);
    }

    public function checkdata()
    {
    	$data = input('post.');

    	model('vercode')->checkcode($data['vercode']);

    	$validate = validate('User');

    	if(!$validate->check($data)){
		    exit(json_encode(['error'=>1,'info'=>$validate->getError()]));
		}

		$data['ctime'] =time();

		$data['password'] =md5($data['password']);
 
        $data['city']  = $this->getCity();

        var_dump($data);

		$res = db('user')->field('nickname,email,password,ctime,city')->insert($data);

		if($res){
			exit(json_encode(['error'=>0,'info'=>"注册成功"]));
		}else{
			exit(json_encode(['error'=>1,'info'=>"注册失败"]));
		}

    }

    public function show()
    {
        $res = model('Vercode')->getRandOne();
        // $data = db('user')->select();
        // 
        

        // var_dump($res);
        // echo "8888";
        // 
       // 模块  模型  模板(前端，视图)
       // 分配变量
       // return $this->fetch('',['res'=>]);
    }

    public function getCity()
    {
        $ip = get_client_ip();

        $ip = "121.33.62.220";

        $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;

        $data = file_get_contents($url);


        $data = json_decode($data,true);


        if($data['code']==0){
            return $data['data']['city'];
        }
    }
}
